<?php

class SiteController extends CController
{
	private $_user;

	public $defaultAction='showuser';

	public function actions()
	{
		return array(
			'login'=>'application.controllers.site.LoginAction',
			'logout'=>'application.controllers.site.LogoutAction',
			'register'=>'application.controllers.site.RegisterAction',
			'showuser'=>'application.controllers.site.ShowUserAction',
			'fight'=>'application.controllers.site.FightAction',
			'listpet'=>'application.controllers.site.ListPetAction',
			'activatepet'=>'application.controllers.site.ActivatePetAction',
			'deletepet'=>'application.controllers.site.DeletePetAction',
			'createpet'=>'application.controllers.site.CreatePetAction', // shoulder be deleted later
			'goto'=>'application.controllers.site.GotoAction',
		);
	}

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('login','register'),
				'users'=>array('?'),
			),
			array('allow',
				'actions'=>array('deletepet','activatepet','createpet'),
				'users'=>array('@'),
				'verbs'=>array('POST'),
			),
			array('deny',
				'actions'=>array('deletepet','activatepet','createpet'),
				'users'=>array('@'),
			),
			array('allow',
				'users'=>array('@'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	public function & getUser()
	{
		if($this->_user===null) {
			$name=Yii::app()->user->name;
			if($name!==null)
				$this->_user=User::loadUser($name);
		}
		return $this->_user;
	}

	public function getUserLocationId()
	{
		$id=Yii::app()->user->getState('userLocationid');
		if(!$id) {
			$id=Location::$default;
			$this->setUserLocationId($id);
		}
		return $id;			
	}

	public function setUserLocationId($id)
	{
		Yii::app()->user->setState('userLocationid',$id);
	}

	public function getUserLocation()
	{
		return $this->getLocation($this->getUserLocationId());
	}

	public function & getActivePet()
	{
		$user=&$this->getUser();
		return Pet::loadPet($user->activePet);
	}
}

